package com.ling.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ling.bean.Msg;
import com.ling.bean.Report;
import com.ling.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class AdminReportController {
    private final ReportService reportService;

    @Autowired
    public AdminReportController(ReportService reportService) {
        this.reportService = reportService;
    }

    //页面跳转,默认，博客类型
    @RequestMapping(value = "/adminReport",method = RequestMethod.GET)
    public String goToAdminReport() {
        return "/admin/adminReport";
    }

    //页面跳转，问题类型
    @GetMapping("/adminReport-question")
    public String goToAdminReportQuestion() {
        return "/admin/adminReport-question";
    }

    //页面跳转，回答类型
    @GetMapping("/adminReport-answer")
    public String goToAdminReportAnswer() {
        return "/admin/adminReport-answer";
    }

    //按照类别查询所有的举报信息，分页
    @GetMapping("/report/{type}/{pn}")
    @ResponseBody
    public Msg getReportByType(@PathVariable Integer type,@PathVariable Integer pn) {
        PageHelper.startPage(pn, 15);
        switch (type) {
            case 1:
                //博客
                List<Report> reportWithBlog = reportService.getReportWithBlog();
                PageInfo<Report> pageInfo = new PageInfo<>(reportWithBlog);
                return Msg.success().add("pageInfo", pageInfo);
            case 2:
                //问题
                List<Report> reportWithQuestion = reportService.getReportWithQuestion();
                PageInfo<Report> pageInfo1 = new PageInfo<>(reportWithQuestion);
                return Msg.success().add("pageInfo", pageInfo1);
            case 3:
                List<Report> reportWithAnswer = reportService.getReportWithAnswer();
                PageInfo<Report> pageInfo2 = new PageInfo<>(reportWithAnswer);
                return Msg.success().add("pageInfo", pageInfo2);
            default:
                return Msg.fail().add("error","获取数据失败，请重新登录！！");
        }
    }

    //举报信息已读操作
    @GetMapping("/report/read/{reportId}")
    @ResponseBody
    public Msg read(@PathVariable String reportId) {
        reportService.read(reportId);
        return Msg.success();
    }
}
